

const STORAGES = {  // 仓库
    "woods" : {
        name: "木头",
        count: 50
    },
    "fish": {
        name: "鱼",
        count: 20
    }

}

const NPCS = { 
    "a" : {
        "name": "a",
        "status": "free",  // 状态
        "energy": {max:100, now: 100},  // 代表饥饿
        "job": {}
    },
    "b" : {
        "name": "b",
        "status": "free",
        "energy": {max:100, now: 100},
        "job": {}
    },
    "c" : {
        "name": "c",
        "status": "free",
        "energy": {max:100, now: 100},
        "job": {}
    },
    "d" : {
        "name": "d",
        "status": "free",
        "energy": {max:100, now: 100},
        "job": {}
    },
}





const AllActions = {  // 所有actions
    "cut a tree": {
        "name" : "砍一颗树",
        "use": {},  // 消耗
        "get": { "woods": 1 },  // 获得
        "process": 10, // 总进度
        "speed": 1    // 速度
    },
    "cook a fish" : {
        "name" : "烤一条鱼",
        "use": { "woods": 1, "fish": 1 },  // 消耗
        "get": { "cookedFish": 1 },  // 获得
        "process": 10, // 总进度
        "speed": 1    // 速度
    }
}

const WorkShops = {  // 工坊
    "kitchen01" : {
        "name": "厨房",
        "status": "free",
        "actions": ["cook a fish"], // 该工坊的 actions
        "orders": [  // 该工坊执行订单
            
        ]
    }
}






const AutoOrder = {   // 自动迭代系统


    orders: [
        {
            "id": "order01",
            "name": "伐木",
            "action": "cut a tree",   // 对应action
            "needWorkShop": false,   // 是否需要工坊
            "loopTime": {  // 循环周期
                "left": 10,
                "max": 10
            },
            "counts": {   // 订单数量
                "left": 10,
                "max": 10
            },
            "condition": [  // 订单条件
                { // 仓库woods 数量 小于 100 时
                    getValue: [{ "type":"STORAGES", "value":"woods"}],   
                    refValue: 100,
                    "type" : "<",   //   <   >
                }
            ]
        }
    ]


}



const AllOrders = [ // 全局订单

]







const GAME = {
    NPCS,STORAGES,WorkShops,AllOrders,AutoOrder,


    run() { // 主迭代
        let that = this;
        setInterval(() => {
            

            // 自动化创建订单系统
            autoOrderUpdate(that)

            // 执行订单
            autoWorkOrder(that)

            // 所有npc迭代
            for(let npcid in that.NPCS) {
                npcUpdate(that.NPCS[npcid])  // 更新npc
            }



        }, 1000);
    }
}






function npcUpdate(npcobj) {
    // npc饥饿每次-1
    npcobj.energy.now--
    if (npcobj.energy.now == 0) { npcobj.energy.now = 0 }
}



// 自动化订单迭代
function autoOrderUpdate(GM) {
    let {AutoOrder, AllOrders,STORAGES} = GM

    let AllOrders_connect_autoOrder_ids = {}

    AllOrders.forEach(order => {
        if (!AllOrders_connect_autoOrder_ids[order.oid]) {
            AllOrders_connect_autoOrder_ids[order.oid] = 1
        } else {
            AllOrders_connect_autoOrder_ids[order.oid]++
        }
    })

    AutoOrder.orders.forEach(order => {
        // 判断是否进入下一次循环
        let loopTime = order.loopTime
        loopTime.left--
        if (loopTime.left > 0) { return }
        loopTime.left = loopTime.max
        // 确定 AllOrders 已经没有该 order
        if (AllOrders_connect_autoOrder_ids[order.id]){ return }

        //// 检查condition条件
        let {condition} = order
        let conditionFlag = true
        for (let index = 0; index < condition.length; index++) {
            let condition_info = condition[index]
            let calcValue = 0; 

            condition_info.getValue.forEach(item => {
                if (item.type == "STORAGES") {
                    calcValue += STORAGES[item.value].count
                }
            })


            if (condition_info.type == "<") {
                if (calcValue < condition_info.refValue) {} else { conditionFlag = false; break }
            } else if (condition_info.type == ">" && calcValue > condition_info.refValue) {
                if (calcValue > condition_info.refValue) {} else { conditionFlag = false; break }
            }
        }


        // 将此订单添加到全局订单列表
        if (conditionFlag) {
            // 判断需不需要查找工坊
            if (order.needWorkShop){
                // todo
            } else {
                AllOrders.push({
                    oid: order.id,
                    name: order.name,
                    needWorkShop: false,   // 是否需要工坊
                    action: order.action,
                    status: "free",
                })
            }
        }

    })
    





}



// 执行订单
function autoWorkOrder(GM) {
    let {AllOrders,STORAGES} = GM

    AllOrders.forEach(order => {
        if(order.status == "free") {
            





        }
    })
    


}









GAME.run()







